<?php
namespace app\index\controller;
use think\Request;
use think\Db;
use think\Validate;

class Index extends Common
{
	public $req;
	function __construct() {
		parent::__construct();
		$this->req = Request::instance();

        // 判断当前控制器, 使相应导航高亮
		$con = __CLASS__;
        $arr = explode('\\', $con);
        $this->assign('_class', strtolower($arr[3]));
	}

    // 主页
    public function index() {
        $index = 1;
        $this->assign('index', $index);

        // 新闻动态
        $newList = Db::query("SELECT `id`,`title`,`publish_at`,`desc`,`main_img` FROM `news` WHERE `status`='1' ORDER BY `publish_at` DESC LIMIT 3");
        $this->assign('newList', $newList);

        // 机型展示
        $jxList = Db::query("SELECT `id`,`title`,`main_img` FROM `articles` WHERE `cid`=9 AND `status`=1");
        $this->assign('jxList', $jxList);

        // 案例展示
        $alList = Db::query("SELECT `id`,`title`,`main_img` FROM `articles` WHERE `cid`=21 AND `status`=1");
        $this->assign('alList', $alList);

        return view('index/index');
    }

    // 联系我们
    public function connect()
    {
        $nav = 'connect';
        $this->assign('nav', $nav);
        return $this->fetch('index/lianxiwomen');
    }

    // 在线留言
    public function message() {
        if ($this->req->isPost()) {
            $data = input('post.');

            // 验证规则+表单令牌
            $rules = [
                'name'    => 'require|max:25|token',
                'email'   => 'email',
                'content' => 'require|max:500',
            ];
            $msg = [
                'name.require'    => '姓名必须',
                'name.max'        => '姓名最多不能超过25个字符',
                'email'           => '邮箱格式错误',
                'content.require' => '内容必须',
                'content.max'     => '内容最多不能超过500个字符',
            ];
            $validate = new Validate($rules);
            if (!$validate->check($data)) {
                return show(1, $validate->getError());
            }

            // 保存数据
            $save = [
                        'name' => $data['name'],
                        'phone' => $data['phone'],
                        'address' => $data['address'],
                        'email' => $data['email'],
                        'content' => $data['content']
                    ];
            $res = Db::table('messages')->insert($save);
            if (!$res) {
                return show(1, '留言失败, 请重试!');
            }
            return show(0, '留言成功!');

        } else {

            $token = $this->req->token('__token__', 'sha1');
            $this->assign('token', $token);

            return $this->fetch('index/zaixianliuyan');
        }
    }
    
    //更多
    public function more($id)
    {
        $name = model("admin/ArticleCategorys")->where('id',$id)->value('name');
        $list = model("admin/Articles")->where('cid',$id)->select();

        return view('index/tupianzhanshi',['list'=>$list,'name'=>$name,'cid'=>$id]);
    }
}
